package com.grey.wolf.starter.security.core;

import cn.hutool.core.util.ObjUtil;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王令
 * @since 2023/12/17
 */
public class Account implements UserDetails {

    @Getter
    private Long id;
    @Getter
    private String username;
    @Getter
    private String password;

    @Getter
    private String phone;

    @Getter
    private String email;
    private Boolean nonLocked;
    private Boolean enabled;

    @Getter
    private List<String> roles;

    private List<String> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (ObjUtil.isEmpty(authorities)) {
            return Collections.emptyList();
        }
        return authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.nonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

}
